home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Utils / makedep / print.c < prev   
C/C++ Source or Header  |  1990-08-16  |  4KB  |  147 lines

  1. /*
  2.  * Module which prints out the dependencies of each source file to the
  3.  * file OutputFileName.
  4.  */
  5.  
  6.  
  7. #include "makedep.h"
  8.  
  9. extern char *rindex();
  10.  
  11. int CurrentMarkValue;        /* This variable is used to keep track of
  12.                    which marking cycle is currently going
  13.                    on.  Each source file's dependency graph
  14.                    is traversed using a new value of
  15.                    this variable.  Dependency records are
  16.                    marked to avoid infinite recursions
  17.                    through a possibly cyclic dependency
  18.                    graph. */
  19.  
  20.  
  21. char lastOFile[50];
  22.  
  23. /*
  24.  * PrintDependencies:
  25.  * Prints out the dependencies of each file on the source file list SrcFiles.
  26.  * This is done by recursively printing the dependency list of each file.
  27.  * I.e. each source file's dependency list is traversed and the print
  28.  * routine is called to print the dependency list of each file on the list.
  29.  * Since there may be cycles in the graph of dependencies, a marking scheme
  30.  * is employed.
  31.  */
  32.  
  33. PrintDependencies()
  34.   {
  35.     StringList *p;
  36.  
  37.     if (Debug)
  38.       {
  39.     printf("\nPrintDependencies:\n");
  40.     PrintList(IList, "IList");
  41.     for (p = SrcFiles->next; p != NULL; p = p->next)
  42.       {
  43.         CheckDepList(p);
  44.       }
  45.       }
  46.  
  47.     /* Mark all include file records' state with the same starting value. */
  48.     for (p = IList->next; p != NULL; p = p->next)
  49.       {
  50.     p->state = START_MARK_VALUE;
  51.       }
  52.     CurrentMarkValue = START_MARK_VALUE;
  53.     /* Traverse the list source files and print the dependencies of each. */
  54.     for (p = SrcFiles->next; p != NULL; p = p->next)
  55.       {
  56.         if (p->dep != NULL)    /* Don't print anything if there are no
  57.                    dependencies! */
  58.       {
  59.         CurrentMarkValue++;
  60.         PrintSrcObjFile(p);
  61.         PrintDeps(p);
  62.         printf("\n");
  63.       }
  64.       }
  65.   }
  66.  
  67.  
  68. /*
  69.  * PrintSrcObjFile:
  70.  * Print the source file name with its source extension replaced by its
  71.  * object extension.  Only the base name is printed.
  72.  */
  73.  
  74. PrintSrcObjFile(p)
  75.     StringList *p;
  76.   {
  77.     char name[80];
  78.     char *ptr;
  79.  
  80.     /* Replace the source file's extension with its object extension. */
  81.     strcpy(name, p->str);
  82.     ptr = name + strlen(name);
  83.     while ((ptr != name) && (*ptr != '.'))
  84.       {
  85.     ptr--;
  86.       }
  87.     if (ptr == name)
  88.       {
  89.     fprintf(stderr, "%s: malformed source file name: %s\n", 
  90.         MyName, p->str);
  91.     exit(1);
  92.       }
  93.     ptr++;            /* Get over the '.' */
  94.     strcpy(ptr, ObjExt);
  95.  
  96.     /* Find the start of the base name. */
  97.     ptr = rindex(name, '/');
  98.     if (ptr != NULL)
  99.       {
  100.         ptr++;
  101.       }
  102.     else
  103.       {
  104.         ptr = name;
  105.       }
  106.  
  107.     /* Print out the first part of the appropriate makefile-style 
  108.        dependency line. */
  109.     if ( lFlag )
  110.     strcpy(lastOFile,ptr);
  111.     else
  112.         printf("%s:", ptr);
  113.   }
  114.  
  115.  
  116. /*
  117.  * PrintDeps:
  118.  * Recursive routine which prints out the dependencies for p and invokes
  119.  * itself for all dependencies of dependencies of p.  Uses a marking scheme
  120.  * to avoid cycles in the dependency graph.
  121.  */
  122.  
  123. PrintDeps(p)
  124.     StringList *p;
  125.   {
  126.     DepList *ptr;
  127.     StringList *p1;
  128.  
  129.     /* Traverse the list of immediate dependencies. */
  130.     for (ptr = p->dep; ptr != NULL; ptr = ptr->next)
  131.       {
  132.         p1 = ptr->inclFile;
  133.         if (p1->state < CurrentMarkValue)
  134.       {            /* We've haven't yet seen this dependency. */
  135.         if ( lFlag )
  136.         printf("%s: %s\n",lastOFile,p1->str);
  137.         else
  138.             printf(" \\\n\t%s", p1->str);
  139.                 /* Print out the immediate dependency. */
  140.         p1->state = CurrentMarkValue;
  141.                 /* Mark the include file as already seen. */
  142.         PrintDeps(p1);
  143.                 /* Print out the dependency's dependencies. */
  144.       }
  145.       }
  146.   }
  147.